package com.dianrui.agent.activity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;


import com.dianrui.agent.R;
import com.dianrui.agent.adapter.MaintenanceOrderListAdapter;
import com.dianrui.agent.bean.Maintenance;
import com.dianrui.agent.okhttp.OkHttpRequest;
import com.dianrui.agent.util.SharedUtil;
import com.dianrui.agent.util.StringUtils;
import com.dianrui.agent.util.ToastUtil;
import com.dianrui.agent.util.Url;
import com.google.gson.JsonObject;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 提交的维修工单
 */
public class MaintenanceOrderListActivity extends BaseActivity {
    private int mPageIndex = 1;
    private List<String> imageList = new ArrayList<>();
    private MaintenanceOrderListAdapter maintenanceOrderListAdapter;
    private List<Maintenance> maintenanceList = new ArrayList<>();

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.emptyView)
    LinearLayout emptyView;

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;


    @Override
    public int getContentViewId() {
        return R.layout.maintenance_order_list;
    }

    @Override
    public void initView() {
        title.setText(getString(R.string.main_menu_fix_txt1));
        maintenanceOrderListAdapter = new MaintenanceOrderListAdapter(MaintenanceOrderListActivity.this, maintenanceList);
        listview.setAdapter(maintenanceOrderListAdapter);
        getRepairList(false);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getRepairList(true);
                refreshLayout.finishRefresh();
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mPageIndex++;
                getRepairList(false);
                refreshLayout.finishLoadMore();
            }
        });
        maintenanceOrderListAdapter.setOnBatteryClickListener(new MaintenanceOrderListAdapter.BatteryClickListener() {
            @Override
            public void onBatteryClick(String order_code, String number, String title, String contents, String create_time, String update_time, JSONArray photo) {
                if (imageList != null) {
                    imageList.clear();
                }
                for (int i = 0; i < photo.length(); i++) {
                    String str = photo.optString(i);
                    imageList.add(str);
                }
                startActivity(new Intent(MaintenanceOrderListActivity.this, FaultDetailsActivity.class)
                        .putExtra("order_code", order_code)
                        .putExtra("number", number)
                        .putExtra("title", contents)
                        .putExtra("contents", contents)
                        .putExtra("create_time", create_time)
                        .putExtra("update_time", update_time)
                        .putStringArrayListExtra("photo", (ArrayList<String>) imageList));
            }
        });

    }

    private void getRepairList(final boolean isRfresh) {
        if (isRfresh) {
            mPageIndex = 1;
        }
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("p", mPageIndex);
        OkHttpRequest.getInstance().postTokenRequests(SharedUtil.getUrl() + Url.MYORDER_MAINTAIN, jsonObject.toString(), new OkHttpRequest.XCallBack() {

            @Override
            public void onResponse(String result) {
                if (isRfresh) {
                    maintenanceList.clear();
                }
                if (!StringUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if ("1000".equals(jsonObject.optString("status"))) {

                            JSONArray jsonArray = jsonObject.optJSONArray("data");
                            if (jsonArray.length() > 0) {
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    Maintenance maintenance = new Maintenance();
                                    maintenance.order_code = jsonArray.optJSONObject(i).optString("order_code");
                                    maintenance.form_name = jsonArray.optJSONObject(i).optString("form_name");
                                    maintenance.number = jsonArray.optJSONObject(i).optString("number");
                                    maintenance.contents = jsonArray.optJSONObject(i).optString("contents");
                                    maintenance.title = jsonArray.optJSONObject(i).optString("title");
                                    maintenance.name = jsonArray.optJSONObject(i).optString("name");
                                    maintenance.create_time = jsonArray.optJSONObject(i).optString("create_time");
                                    maintenance.update_time = jsonArray.optJSONObject(i).optString("update_time");
                                    maintenance.state = jsonArray.optJSONObject(i).optString("state");
                                    maintenance.photo = jsonArray.optJSONObject(i).optJSONArray("photo");
                                    maintenanceList.add(maintenance);
                                }
                                if (maintenanceOrderListAdapter != null) {
                                    maintenanceOrderListAdapter.refresh(maintenanceList);
                                }
                                refreshLayout.finishRefresh();

                            } else {
                                ToastUtil.showToast(getString(R.string.no_fix_list));
                            }
                        } else if ("1001".equals(jsonObject.optString("status"))) {
                            ToastUtil.showToast(jsonObject.optString("message"));
                        } else if ("1002".equals(jsonObject.optString("status"))) {
                            spUtils.clear();
                            ToastUtil.showToast(jsonObject.optString("message"));
                            JumpActivitys(LoginActivity.class);
                            finish();
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    if (maintenanceList.size() <= 0) {
                        refreshLayout.finishLoadMoreWithNoMoreData();
                        emptyView.setVisibility(View.VISIBLE);
                        listview.setVisibility(View.GONE);
                    } else {
                        emptyView.setVisibility(View.GONE);
                        listview.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            public void FailedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }


    @OnClick({R.id.back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (maintenanceList != null) {
            maintenanceList.clear();
        }
    }
}
